package com.colin.controller;

import com.colin.pojo.Users;
import com.colin.service.ex.ServiceException;
import com.colin.service.impl.UsersServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/public")
public class PublicController extends BaseController {
    @Autowired
    UsersServiceImpl usersServiceImpl;
    @RequestMapping("/login")
    public String login(@RequestParam(value = "redirect" , required = false , defaultValue = "") String redirect , HttpSession session){
        if (!redirect.isEmpty()){
            System.out.println("redirect=>" + redirect);
            session.setAttribute("redirect" , redirect);
        }
        return "public/login";
    }

    @PostMapping("/login")
    public String loginPost(String user , String pass , Model model , HttpSession session){
        if (user.equals("")){
            model.addAttribute("error" , "用户名不能为空");
            return "public/error";
        }
        if (pass.equals("")){
            model.addAttribute("error" , "密码不能为空");
            return "public/error";
        }
        // 获取当前用户
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user, pass);
        try {
            subject.login(token);
            Object redirect = session.getAttribute("redirect");
            if (redirect != null){
                session.removeAttribute("redirect");
                return "redirect:" + redirect;
            }
            return "redirect:/member/index";
        } catch (ServiceException e) {
            model.addAttribute("error" , e.getMessage());
            return "public/error";
        }
    }

    @RequestMapping("/register")
    public String register(){
        return "public/register";
    }

    @PostMapping("/register")
    public String registerPost(String user , String pass , String repass , Model model , HttpSession session){
        if (user.equals("") || pass.equals("") || repass.equals("")){
            model.addAttribute("error" , "用户名或密码未填写");
            return "public/error";
        }
        if (!pass.equals(repass)){
            model.addAttribute("error" , "两次密码输入不一致");
            return "public/error";
        }
        Users userFind = usersServiceImpl.queryByUserName(user);
        if (userFind != null){
            model.addAttribute("error" , "用户名已经被使用了");
            return "public/error";
        }
        int res = usersServiceImpl.insertUser(user , pass);
        if (res > 0){
            session.setAttribute("userInfoId" , res);
            return "redirect:/";
        }else{
            model.addAttribute("error" , "注册失败");
            return "public/error";
        }
    }

    @RequestMapping("/noauth")
    @ResponseBody
    public String noauth(){
        return "notauth";
    }

    @RequestMapping("/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/public/login";
    }
}
